<?php
require_once('../../config.php');
//echo "wwwroot is now $wwwroot";
//echo "dirroot is now $dirroot";


//require_once('./security.php');

  if (isset($_POST['submit0'])) { // If password entered...
    //echo "password was entered<br>";
    $word = $_POST['password']; // Variable set as entered password
    setcookie('textadmin', $word, time() + 31536000); // Cookie is set
  } else { // If password form filled in previously...
    //echo "password not submitted<br>";
    if (isset($_COOKIE['textadmin'])) { // Check for cookie
      //echo "cookie is set<br>";
    
      $word = $_COOKIE['textadmin']; // Variable set as cookie
      
    }else{
      //echo "cookie is not set<br>";  
    }
  }

  // Retrieve keywords for a specified page
  if (isset($_POST['submit1'])) { // Handle the form
    // Convert case and trim ends
    $page = trim(strtolower($_POST['thispage']));
    // Strip out extra spaces in search string
    $page = preg_replace("/[ ]+/", " ", $page);
    // Check something was entered
    $number = str_word_count($page);
    if ($number < 1) {
      $problem = true;
      $response = "You didn't enter a page name.\n";
    } else {
      $problem = false;
    }
    // If something was entered...
    if (!$problem) {
      $filename = "../search/index.txt";
      $fp = fopen($filename, "r") or die("Couldn't open $filename.");
      // Loop through all the lines
      while (!feof($fp)) {
        $line = fgets($fp, 1024);
        // Check each line for search term
        if (preg_match("/\/$page\b/", $line)) { // \b = boundary
          $result = true;
          // Convert line string into array using | as separator
          $lineArray = explode("|", $line);

          // Get the page title from $lineArray
          $pagetitle = $lineArray[1];

          // Slice off after 2nd element to new array
          $keywordArray = array_slice($lineArray, 2);
          // Convert back to comma-separated string
          $keywordString = implode(", ", $keywordArray);
        } // End of match conditional
      } // End of while loop
      if (!$result) {
        $response = "Sorry, no keywords were found for <b>$page</b>.<br />Perhaps there is no page with that name.";
      }
    }
  }

  // Edit keywords
  if (isset($_POST['submit2'])) { // Handle the form

    // Retrieve data and format variables

    // (1) Get posted values
    $page = $_POST['thispage'];
    // Target string to be replaced
    $existingKeywords = $_POST['existingKeywords']; // From hidden field
    // Replacement string
    $keywords = $_POST['keywords'];

    // (2) Convert "comma+space" to "|"
    $existingKeywords = str_replace(", ", "|", $existingKeywords);
    $existingKeywords = trim($existingKeywords);
    $keywords = str_replace(", ", "|", $keywords);
    $keywords = trim($keywords);

    // (3) Strip out extra spaces in keywords string
    $newKeywords = preg_replace("/[ ]{2,}/", "", $keywords);

    // (4) Set the path to the file
    $filename = "../search/index.txt";

    // (5) Get the file contents as an array
    $fileArray = file($filename);

    // Get the Page Title for $page
    foreach ($fileArray as $val) {
      // Check each $val for $page
      if (preg_match("/\/$page\b/", $val)) { // \b = boundary
        // Convert val string into array
        $valArray = explode("|", $val);
        // Get second element
        $pagetitle = $valArray[1];
      } // End of match conditional
    }

    // Do the update

    // Open the file for writing
    $fp = fopen($filename, "w") or die("Couldn't open $filename.");

    // Convert $fileArray to string for writing
    $contents = implode($fileArray);

    // Update keywords: in $contents, replace revised substring
    $contents2 = trim(str_replace($existingKeywords, $newKeywords, $contents)); // Not working

    // Write new content
    fwrite($fp, $contents2);
    fclose($fp);
    $response = "The keywords were updated.";
  }

  // Delete page
  if (isset($_POST['submit3'])) { // Handle the form
    // Set the page
    $page = $thispage;
    // Convert strings to index.txt format
    $existingKeywords = str_replace(", ", "|", $existingKeywords);
    // Add the page and page title
    $toDelete = "/$page|$pagetitle|$existingKeywords";
    // Set the path to the file
    $filename = "../search/index.txt";
    // Get the file contents as an array
    $fileArray = file($filename);
    // Convert array to string
    $contents = implode($fileArray);
    // Write new, omitting deleted string
    $contents = str_replace($toDelete, "", $contents);
    $contents = trim($contents);
    // Open the file for writing
    $fp = fopen($filename, "w") or die("Couldn't open $filename.");
    // Write new content
    fwrite($fp, $contents);
    fclose($fp);
    $response = "The page keywords were deleted.";
  }

?>

<?php $thisPage = "navigation"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Edit search index</title>
<meta name="robots" content="noindex,nofollow" />
<?php require_once('theme/admintheme.php');?>
</head>
<body>

<div id="wrap">

<?php

// Start password routine
  //TG 19/7/09
  include($dirroot.'/password.php');
  //echo "about to enter password routine";
  //echo "word is $word<br>";
  //echo "le_password is $le_password";
  if ($word != $le_password) {
     //echo "word is not equal to the password<br>";

?>

<!-- password form //-->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<b>Enter password:</b>
<br />
<input type="password" name="password" size="25" maxlength="25" />
<br />
<input type="submit" name="submit0" value="Submit" />
</p>
</form>
<!-- password form_eof //-->

<?php

    // Routine to display password entry error message.
    if (isset($_POST['submit0'])) {
      if ($_POST['password'] != $le_password) {
        echo '<p">Incorrect password!</p>';
      }
    }

  } else {
// End password routine

?>

<?php include_once('navigation.php'); ?>



<?php

  } // Required for password routine

?>

</div>

</body>
</html>






